﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ATM.Models;
using System.Data;
namespace ATM.Controllers
{
    public class ATMController : Controller
    {
        ATMEntities Ae = new ATMEntities();
        public ActionResult Index()
        {
            return View();
        }
       // Validation
        [HttpPost]
        public ActionResult CheckCardNo(string txtNhap)
        {
            string cardNo = Convert.ToString(txtNhap);
            var query = (from C in Ae.Cards
                              where C.CardNo == cardNo
                              select C).FirstOrDefault();
            if(query.status == "Block")
            {
                return RedirectToAction("SwallowCard");
            }
            if (query!=null)
            {
                Session["card"] = query.CardNo;
                Session["pin"] = query.PIN;
                Session["attempt"] = query.Attempt;
                Session["Status"] = query.status;
                //transfer cash
                var account = (from a in Ae.Accounts
                               where a.AccountID == query.AccountID
                               select a).FirstOrDefault();
                var customer = (from c in Ae.Customers
                                where c.CustID == account.CustID
                                select c).FirstOrDefault();
                Session["CustomerName"] = customer.Name;
                Session["CustID"] = customer.CustID;
                Session["BalanceTransfer"] = account.Balance;
                Session["AccountTransferID"] = account.AccountID;

                //end
                return RedirectToAction("Authentication");
            }
            return RedirectToAction("Index");
        }
        public ActionResult CheckPin(string txtNhap)
        {
            String cardNo = Session["card"].ToString();
            var query = (from C in Ae.Cards
                         where C.CardNo == cardNo
                         select C).FirstOrDefault();
            String pin = Convert.ToString(txtNhap);
            String getPin = Session["pin"].ToString();
            int attempt = int.Parse(Session["attempt"].ToString());
            if (attempt < 2)
            {
                if (pin == getPin)
                {
                    return RedirectToAction("SelectTransaction");
                }
                else
                {
                    Session["attempt"] = Convert.ToInt32(Session["attempt"]) + 1;
                    query.Attempt += 1;
                    Ae.Entry(query).State = EntityState.Modified;
                    Ae.SaveChanges();
                    return RedirectToAction("Authentication");
                }
            }
            else
            {
                query.status = "Block";
                Ae.Entry(query).State = EntityState.Modified;
                Ae.SaveChanges();
                return RedirectToAction("SwallowCard");
            }
        }

        public ActionResult Authentication()
        {
            
            return View();
        }
        public ActionResult SelectTransaction()
        {
            return View();
        }
        public ActionResult SwallowCard()
        {

            return View();
        }
        //End Validation
        // Withdraw
        public ActionResult CheckConfig(string Command)
        {
            if (Command == "Button 1")
            {
                decimal amount = 500000;
                int cardTT = Convert.ToInt32(Session["CustID"]);
                var query = (from A in Ae.Accounts
                             where A.CustID == cardTT
                             select A).First();
                if ((500000 <= Convert.ToInt32(query.Balance)) && (Convert.ToInt32(query.Balance) - 500000 >= 50000))
                {

                    query.Balance = query.Balance - 500000;
                    Ae.Entry(query).State = EntityState.Modified;
                    Ae.SaveChanges();
                    var logs = (from A in Ae.Logs
                                select A).First();
                    Log log = new Log();
                    log.LogTypeID = 1;
                    log.ATMID = 1;
                    log.CardNo = Convert.ToString(Session["card"]);
                    log.LogDate = DateTime.Now;
                    log.Amount = amount;
                    log.Details = "";
                    Ae.Logs.Add(log);
                    Ae.SaveChanges();
                    return RedirectToAction("Printreceipt");
                }
                else
                {
                    return RedirectToAction("NotifyBalance");
                }
            }
            if (Command == "Button 2")
            {
                decimal amount = 2000000;
                int cardTT = Convert.ToInt32(Session["CustID"]);
                var query = (from A in Ae.Accounts
                             where A.CustID == cardTT
                             select A).First();
                if ((2000000 <= Convert.ToInt32(query.Balance)) && (Convert.ToInt32(query.Balance) - 2000000 >= 50000))
                {
                    query.Balance = query.Balance - 2000000;
                    Ae.Entry(query).State = EntityState.Modified;
                    Ae.SaveChanges();
                    var logs = (from A in Ae.Logs
                                select A).First();
                    Log log = new Log();
                    log.LogTypeID = 1;
                    log.ATMID = 1;
                    log.CardNo = Convert.ToString(Session["card"]);
                    log.LogDate = DateTime.Now;
                    log.Amount = amount;
                    log.Details = "";
                    Ae.Logs.Add(log);
                    Ae.SaveChanges();
                    return RedirectToAction("Printreceipt");
                }
                else
                {
                    return RedirectToAction("NotifyBalance");
                }
            }
            if (Command == "Button 3")
            {
                decimal amount = 3000000;
                int cardTT = Convert.ToInt32(Session["CustID"]);
                var query = (from A in Ae.Accounts
                             where A.CustID == cardTT
                             select A).First();
                if ((3000000 <= Convert.ToInt32(query.Balance)) && (Convert.ToInt32(query.Balance) - 3000000 >= 50000))
                {
                    query.Balance = query.Balance - 3000000;
                    Ae.Entry(query).State = EntityState.Modified;
                    Ae.SaveChanges();
                    var logs = (from A in Ae.Logs
                                select A).First();
                    Log log = new Log();
                    log.LogTypeID = 1;
                    log.ATMID = 1;
                    log.CardNo = Convert.ToString(Session["card"]);
                    log.LogDate = DateTime.Now;
                    log.Amount = amount;
                    log.Details = "";
                    Ae.Logs.Add(log);
                    Ae.SaveChanges();
                    return RedirectToAction("Printreceipt");
                }
                else
                {
                    return RedirectToAction("NotifyBalance");
                }
            }
            return View();
        }
        public ActionResult Money(string txtNhapSoTien)
        {

            decimal amount = Convert.ToDecimal(txtNhapSoTien);
            if (amount == 0)
            {
                return RedirectToAction("Notifywithdraw");
            }
            else
            {
                if ((50000 <= amount) && (amount <= 3000000))
                {
                    int cardTT = Convert.ToInt32(Session["CustID"]);
                    var query = (from A in Ae.Accounts
                                 where A.CustID == cardTT
                                 select A).First();
                    if ((amount <= Convert.ToInt32(query.Balance)) && (Convert.ToInt32(query.Balance) - amount >= 50000) && (amount % 50000 == 0))
                    {
                        query.Balance = query.Balance - amount;
                        Ae.Entry(query).State = EntityState.Modified;
                        Ae.SaveChanges();
                        var logs = (from A in Ae.Logs
                                    // where A.CardNo == A.CardNo
                                    select A).FirstOrDefault();
                        Log log = new Log();
                        log.LogTypeID = 1;
                        log.ATMID = 1;
                        log.CardNo = Convert.ToString(Session["card"]);
                        log.LogDate = DateTime.Now;
                        log.Amount = amount;
                        log.Details = "";
                        Ae.Logs.Add(log);
                        Ae.SaveChanges();
                        return RedirectToAction("printreceipt");
                    }
                    else
                    {
                        return RedirectToAction("Notifywithdraw");
                    }
                }
                else
                {
                    return RedirectToAction("Notifywithdraw");
                }
            }
        }


        public ActionResult Withdraw()
        {
            return View("Withdraw");
        }
        public ActionResult Enterother(string Command)
        {
            if (Command == "Button 4")
            {
                decimal amount = 1000000;
                int cardTT = Convert.ToInt32(Session["CustID"]);
                var query = (from A in Ae.Accounts
                             where A.CustID == cardTT
                             select A).First();
                if ((1000000 <= Convert.ToInt32(query.Balance)) && (Convert.ToInt32(query.Balance) - 1000000 >= 50000))
                {

                    query.Balance = query.Balance - 1000000;
                    Ae.Entry(query).State = EntityState.Modified;
                    Ae.SaveChanges();
                    var logs = (from A in Ae.Logs
                                select A).First();
                    Log log = new Log();
                    log.LogTypeID = 1;
                    log.ATMID = 1;
                    log.CardNo = Convert.ToString(Session["card"]);
                    log.LogDate = DateTime.Now;
                    log.Amount = amount;
                    log.Details = "";
                    Ae.Logs.Add(log);
                    Ae.SaveChanges();
                    return RedirectToAction("Printreceipt");
                }
                else
                {
                    return RedirectToAction("NotifyBalance");
                }
            }
            if (Command == "Button 5")
            {
                decimal amount = 2500000;
                int cardTT = Convert.ToInt32(Session["CustID"]);
                var query = (from A in Ae.Accounts
                             where A.CustID == cardTT
                             select A).First();
                if ((2500000 <= Convert.ToInt32(query.Balance)) && (Convert.ToInt32(query.Balance) - 2500000 >= 50000))
                {

                    query.Balance = query.Balance - 2500000;
                    Ae.Entry(query).State = EntityState.Modified;
                    Ae.SaveChanges();
                    var logs = (from A in Ae.Logs
                                select A).First();
                    Log log = new Log();
                    log.LogTypeID = 1;
                    log.ATMID = 1;
                    log.CardNo = Convert.ToString(Session["card"]);
                    log.LogDate = DateTime.Now;
                    log.Amount = amount;
                    log.Details = "";
                    Ae.Logs.Add(log);
                    Ae.SaveChanges();
                    return RedirectToAction("Printreceipt");
                }
                else
                {
                    return RedirectToAction("NotifyBalance");
                }
            }
            if (Command == "Button 6")
            {
                return RedirectToAction("Enterother");
            }
            return View();
        }
        public ActionResult Printreceipt()
        {
            return View("Printreceipt");
        }
        public ActionResult Notifywithdraw()
        {
            return View("Notifywithdraw");
        }
        public ActionResult Cancel()
        {
            return View("SelectTransaction");
        }
        public ActionResult Continue()
        {
            return View("Withdraw");
        }
        public ActionResult NotifyBalance()
        {
            return View("NotifyBalance");
        }
        public ActionResult trave()
        {
            return View("SelectTransaction");
        }

        // End Withdraw
        // check Balance
        public ActionResult CheckBalance()
        {
            String cardNo = Session["card"].ToString();
            Card card = new Card();
            card = Ae.Cards.Single(t => t.CardNo.Equals(cardNo));
            int accountID = Convert.ToInt32(card.AccountID);

            Account ac = new Account();
            ac = Ae.Accounts.Single(t => t.AccountID.Equals(accountID));


            return View(ac);
        }
        public ActionResult OK_PrintBill()
        {

            return View();
        }
        public ActionResult Thankyou()
        {
            return View();
        }
        public ActionResult ScreenChangePIN()
        {
            return View();
        }
        public ActionResult ChangePIN(String txtNhap)
        {
            String newPIN = txtNhap;
            if (String.Compare(newPIN, Session["pin"].ToString()) == 0)
            {
                return RedirectToAction("ScreenChangePIN");
            }
            else if (newPIN.Length == 6 )
            {
                Session["newPIN"] = newPIN;
                return View("Re_EnterPIN");
            }
            else
            {
                return View("ScreenChangePIN");
            } 
        }
        public ActionResult Re_EnterPIN()
        {
            return View();
        }
        public ActionResult CheckNewPIN(String txtNhapLai)
        {
            String cardID = Session["card"].ToString();
            Card card = Ae.Cards.Single(t => t.CardNo.Equals(cardID));
            if (String.Compare(txtNhapLai, Session["newPIN"].ToString()) == 1)
            {
                return RedirectToAction("ScreenChangePIN");
            }
            else if (txtNhapLai.Length == 6 )
            {
                    card.PIN = txtNhapLai;
                    Ae.Entry(card).State = EntityState.Modified;
                    Ae.SaveChanges();
                    return View("ChangePIN_Succesful");
            }
            return View("ScreenFlasePIN");
        }
        public ActionResult ChangePIN_Succesful()
        {
            return View();
        }
        public ActionResult ScreenFlasePIN()
        {
            return View();
        }
        // end check Balance
        // Transfer Cash
        public ActionResult Transfer()
        {
            return View();
        }
        public ActionResult EnterReID()
        {
            return View();
        }
        public ActionResult DisplayReInfo()
        {
            return View();
        }
        public ActionResult ANotExist()
        {
            return View();
        }
        public ActionResult CheckReID(string txtNhap)
        {
            string checka = Convert.ToString(txtNhap);
            var query2 = (from A in Ae.Cards
                          where A.CardNo == checka
                          select A).FirstOrDefault();
            if (checka == Convert.ToString(Session["card"]))
            {
                return RedirectToAction("ANotExist");
            }
            if (query2 != null)
            {
                var account = (from a in Ae.Accounts
                               where a.AccountID == query2.AccountID
                               select a).FirstOrDefault();
                var customer = (from c in Ae.Customers
                                where c.CustID == account.CustID
                                select c).FirstOrDefault();
                Session["card2"] = query2.CardNo;
                Session["CustomerName2"] = customer.Name;
                Session["BalanceReceive"] = account.Balance;
                Session["AccountReceiveID"] = query2.AccountID;
                return RedirectToAction("DisplayReInfo");
            }
            return RedirectToAction("ANotExist");
        }
        public ActionResult EnterAmount()
        {
            return View();
        }
        public ActionResult ReAmount()
        {
            return View();
        }
        public ActionResult CheckAmount(string txtNhap)
        {
            if (txtNhap == null || txtNhap == "")
            {
                return RedirectToAction("EnterAmount");
            }
            else
            {
                decimal checkAmount = Convert.ToDecimal(txtNhap);
                decimal ba_transfer = Convert.ToDecimal(Session["BalanceTransfer"]);
                decimal ba_receive = Convert.ToDecimal(Session["BalanceReceive"]);
                int atID = Convert.ToInt32(Session["AccountTransferID"]);
                int arID = Convert.ToInt32(Session["AccountReceiveID"]);
                if (checkAmount < ba_transfer && checkAmount>=50000)
                {
                    Session["Amount"] = checkAmount;
                    return RedirectToAction("DisplayAmount");
                }
            }
            return RedirectToAction("EnterAmount");
        }
        public ActionResult DisplayAmount()
        {
            return View();
        }
        public ActionResult Print()
        {
            decimal saveAmount = Convert.ToDecimal(Session["Amount"]);
            if (Session["Amount"] != null)
            {
                int atID = Convert.ToInt32(Session["AccountTransferID"]);
                int arID = Convert.ToInt32(Session["AccountReceiveID"]);

                var account = (from a in Ae.Accounts
                               where a.AccountID == arID
                               select a).FirstOrDefault();
                var account1 = (from a in Ae.Accounts
                                where a.AccountID == atID
                                select a).FirstOrDefault();
                account.Balance = account.Balance + saveAmount;
                account1.Balance = account1.Balance - saveAmount;
                Ae.Entry(account).State = EntityState.Modified;
                Ae.Entry(account1).State = EntityState.Modified;
                Ae.SaveChanges();
                var logs = (from A in Ae.Logs
                            select A).First();
                Log log = new Log();
                log.LogTypeID = 2;
                log.ATMID = 1;
                log.CardNo = Convert.ToString(Session["card"]);
                log.LogDate = DateTime.Now;
                log.Amount = saveAmount;
                log.Details = "";
                log.AccountReceiver = account1.AccountNo;
                Ae.Logs.Add(log);
                Ae.SaveChanges();
            }
            return View();
        }
        // end Transfer
    }
}
